from BeautifulSoup import BeautifulSoup
import urllib2

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext import db

class ParseTeamTable(webapp.RequestHandler):
    def get(self):
        url = 'http://www.formula1.com/results/team/'
        req = urllib2.urlopen(url)
        page = req.read()
        soup = BeautifulSoup(page)

        table = soup.find("table", { "class" : "raceResults" })

        trs = table.findAll(name = "tr")

        for tr in trs:
            tds = tr.findAll(name = "td")
            if len(tds)>0:
                row = TeamTableRow()
                row.position = int(tds[0].string)
                row.team     = str(tds[1].next.string)
                row.points   = int(tds[2].string)
                row.put()

class TeamTableRow(db.Model):
    position = db.IntegerProperty()
    team     = db.StringProperty()
    points   = db.IntegerProperty()

application = webapp.WSGIApplication([('/', ParseTeamTable)],debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()